package com.cinorecycle.printqrcode.util;

import com.cinorecycle.printqrcode.common.PrintParam;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.time.LocalDate;

public class TextQrCode {
    /**
     * @param pressText 文字
     * @param newImg    带文字的图片
     * @param image     需要添加文字的图片
     * @param fontStyle
     * @param color
     * @param fontSize
     * @param width
     * @param height
     * @为图片添加文字
     */
    public static void pressText(String pressText, String newImg, BufferedImage image, int fontStyle, Color color, int fontSize, int width, int height) {

        LocalDate companyStr = LocalDate.now();
        int startCompanyX = 16;
        int startCompanyY = height - 6;

        try {
//            File file = new File(targetImg);
//            Image src = ImageIO.read(file);
            int imageW = image.getWidth();
            int imageH = image.getHeight();
//            BufferedImage image = new BufferedImage(imageW, imageH, BufferedImage.TYPE_INT_RGB);
            Graphics g = image.createGraphics();
            g.drawImage(image, 0, 0, imageW, imageH, null);
            g.setColor(color);
            g.setFont(new Font(PrintParam.fontName, Font.BOLD, 15));


            g.drawString(companyStr+"", startCompanyX, startCompanyY);
            //g.drawString("CPU", startX + 30, startY + 30);
            g.dispose();
            System.out.println("image press success");
        } catch (
                Exception e) {
            e.printStackTrace();
            System.out.println(e);
        }

        //计算文字开始的位置
        //x开始的位置：（图片宽度-字体大小*字的个数）/2
        //int startX = (width - (fontSize * pressText.length())) - 10;
        int startX = width - height - 15;
        //y开始的位置：图片高度-（图片高度-图片宽度）/2
        int startY = PrintParam.y;

        String[] texts = pressText.trim().split("\n");

        try {
//            File file = new File(targetImg);
//            Image src = ImageIO.read(file);
            int imageW = image.getWidth();
            int imageH = image.getHeight();
//            BufferedImage image = new BufferedImage(imageW, imageH, BufferedImage.TYPE_INT_RGB);
            Graphics g = image.createGraphics();
            g.drawImage(image, 0, 0, imageW, imageH, null);
            g.setColor(color);
            for (int i = 0; i < texts.length; i++) {
                String text = texts[i];
                /*if (i == 0) {
                    g.setFont(new Font(PrintParam.fontName, Font.BOLD, PrintParam.otherSize));
                    g.drawString(text, startX, startY);
                    startY += PrintParam.clearance;
                } else {*/
                    g.setFont(new Font(PrintParam.fontName, Font.BOLD, PrintParam.otherSize - 4));

                    if (text.length() <= 22) {
                        g.setFont(new Font(PrintParam.fontName, Font.BOLD, PrintParam.otherSize - 4));
                        g.drawString(text, startX, startY);
                        startY += PrintParam.clearance;
                    } else if (text.length() <= 27) {
                        g.drawString(text, startX, startY);
                        startY += PrintParam.clearance;
                    } else if (text.length() <= 60) {
                        String s1 = text.substring(0, 27);
                        String s2 = text.substring(27);
                        g.drawString(s1, startX, startY);
                        startY += PrintParam.clearance;
                        g.drawString(s2, startX, startY);
                        startY += PrintParam.clearance;
                    } else {
                        String s1 = text.substring(0, 27);
                        String s2 = text.substring(27, 54);
                        String s3 = text.substring(54);
                        g.drawString(s1, startX, startY);
                        startY += PrintParam.clearance;
                        g.drawString(s2, startX, startY);
                        startY += PrintParam.clearance;
                        g.drawString(s3, startX, startY);
                        startY += PrintParam.clearance;
                    }


//                }
            }
            //g.drawString("CPU", startX + 30, startY + 30);
            g.dispose();

            FileOutputStream out = new FileOutputStream(newImg);
            ImageIO.write(image, "JPEG", out);
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            encoder.encode(image);
            out.close();
            System.out.println("image press success");
        } catch (
                Exception e) {
            e.printStackTrace();
            System.out.println(e);
        }
    }

    public static void pressTextPrintQr(String pressText, String newImg, BufferedImage image, int fontStyle, Color color, int fontSize, int width, int height) {

        String companyStr = "www.cinorecycle.com";
        int startCompanyX = 16;
        int startCompanyY = height - 6;

        try {
//            File file = new File(targetImg);
//            Image src = ImageIO.read(file);
            int imageW = image.getWidth();
            int imageH = image.getHeight();
//            BufferedImage image = new BufferedImage(imageW, imageH, BufferedImage.TYPE_INT_RGB);
            Graphics g = image.createGraphics();
            g.drawImage(image, 0, 0, imageW, imageH, null);
            g.setColor(color);
            g.setFont(new Font(PrintParam.fontName, Font.BOLD, 15));


            g.drawString(companyStr, startCompanyX, startCompanyY);
            //g.drawString("CPU", startX + 30, startY + 30);
            g.dispose();
            System.out.println("image press success");
        } catch (
                Exception e) {
            e.printStackTrace();
            System.out.println(e);
        }

        //计算文字开始的位置
        //x开始的位置：（图片宽度-字体大小*字的个数）/2
        //int startX = (width - (fontSize * pressText.length())) - 10;
        int startX = width - height + 5;
        //y开始的位置：图片高度-（图片高度-图片宽度）/2
        int startY = PrintParam.y + 40;

        String[] texts = pressText.trim().split("\n");

        try {
//            File file = new File(targetImg);
//            Image src = ImageIO.read(file);
            int imageW = image.getWidth();
            int imageH = image.getHeight();
//            BufferedImage image = new BufferedImage(imageW, imageH, BufferedImage.TYPE_INT_RGB);
            Graphics g = image.createGraphics();
            g.drawImage(image, 0, 0, imageW, imageH, null);
            g.setColor(color);
            for (int i = 0; i < texts.length; i++) {
                String text = texts[i];
                g.setFont(new Font(PrintParam.fontName, Font.BOLD, 38));
                g.drawString(text, startX, startY);
                startY += PrintParam.clearance;
            }
            //g.drawString("CPU", startX + 30, startY + 30);
            g.dispose();

            FileOutputStream out = new FileOutputStream(newImg);
            ImageIO.write(image, "JPEG", out);
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            encoder.encode(image);
            out.close();
            System.out.println("image press success");
        } catch (
                Exception e) {
            e.printStackTrace();
            System.out.println(e);
        }
    }
}
